﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillData();
            }
            else
            {

            }
        }
        private void FillData()
        {
            var bt = string.Format("select Id,Title,Content,Author,Remarks from Articles");
            var be = Dbhepr.dataTable(bt);
            Gv_one.DataSource = be;
            Gv_one.DataBind();
        }
        private string  GetValue(int a ,int b)
        {
            var bt = Gv_one.Rows[a].Cells[b];
            var be = bt.Controls.Count > 0 ? ((TextBox)bt.Controls[0]).Text : bt.Text;
            return be;
        }
        protected void Gv_one_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            Gv_one.EditIndex = -1;
            FillData();
        }

        protected void Gv_one_RowEditing(object sender, GridViewEditEventArgs e)
        {
            Gv_one.EditIndex = e.NewEditIndex;
            FillData();
        }

        protected void Gv_one_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var id = GetValue(e.RowIndex, 0);
            var bt = string.Format("delete from Articles where Id='{0}'",id);
            Dbhepr.OrUpDataOrDelete(bt);
            Gv_one.EditIndex = -1;
            FillData();

        }

        protected void Gv_one_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            var title = GetValue(e.RowIndex, 1);
            var content = GetValue(e.RowIndex, 2);
            var author = GetValue(e.RowIndex, 3);
            var remarks = GetValue(e.RowIndex, 4);
            var id = GetValue(e.RowIndex, 0);
            var bt = string.Format("Update Articles set Title='{0}',Content='{1}',Author='{2}',Remarks='{3}' where Id='{4}'",title,content,author,remarks,id);
            Dbhepr.OrUpDataOrDelete(bt);
            Gv_one.EditIndex = -1;
            FillData();
                
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("Add.aspx");
        }
    }
}